package com.bci.pwtz.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.exception.PwtzException;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.mysql.model.Zydlfzx;
import com.bci.pwtz.mysql.model.Zyhwg;
import com.bci.pwtz.mysql.model.Zykgz;
import com.bci.pwtz.mysql.model.Zypds;
import com.bci.pwtz.mysql.model.Zyxl;
import com.bci.pwtz.mysql.model.Zyxsbdz;
import com.bci.pwtz.mysql.model.Zyzndlq;
import com.bci.pwtz.service.BusinessCommonService;
import com.bci.pwtz.service.ZydlfzxService;
import com.bci.pwtz.service.ZyhwgService;
import com.bci.pwtz.service.ZykgzService;
import com.bci.pwtz.service.ZypdsService;
import com.bci.pwtz.service.ZyxlService;
import com.bci.pwtz.service.ZyxsbdzService;
import com.bci.pwtz.service.ZyzndlqService;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("/user/zyzndlq")
public class ZyzndlqController extends AbstractController
{
    @Autowired
    private ZyzndlqService service;
    @Autowired
    ZyxlService lineService;
    @Autowired
    ZykgzService zykgzService;
    @Autowired
    ZypdsService zypdsService;
    @Autowired
    ZyhwgService zyhwgService;
    @Autowired
    ZyxsbdzService zyxsbdzService;
    @Autowired
    ZydlfzxService zydlfzxService;
    @Autowired
    private BusinessCommonService businessCommonService;
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public PaginationList<Zyzndlq> list(String stationcode,int stationdevicetype,String deviceName,int pageSize,int pageNo) throws PwtzException
    {

        PaginationList<Zyzndlq> list = new PaginationList<Zyzndlq>();
        try
        {
            Long companyid = getLastCompanyId();
            String stationname="";
            if(stationdevicetype==124){
                Zykgz device=zykgzService.load(stationcode);
                if(device!=null){
                    stationname=device.getDevicename();
                }
            }else if(stationdevicetype==125){
                Zypds device=zypdsService.load(stationcode);
                if(device!=null){
                    stationname=device.getDevicename();
                }
            }else if(stationdevicetype==126){
                Zyhwg device=zyhwgService.load(stationcode);
                if(device!=null){
                    stationname=device.getDevicename();
                }
            }else if(stationdevicetype==127){
                Zyxsbdz device=zyxsbdzService.load(stationcode);
                if(device!=null){
                    stationname=device.getDevicename();
                }
            }else if(stationdevicetype==128){
                Zydlfzx device=zydlfzxService.load(stationcode);
                if(device!=null){
                    stationname=device.getDevicename();
                }
            }
            list = service.selectDeviceByZf(stationcode,stationdevicetype,deviceName, companyid, pageSize, pageNo);
            if(list!=null&&list.getRecords()!=null&&list.getRecords().size()>0){
                for(Zyzndlq device : list.getRecords()){
                    Zyxl line=lineService.load(device.getLinecode());
                    if(line!=null){
                        device.setLinecode(line.getDevicename());
                    }else{
                        device.setLinecode("");
                    }
                    device.setStationcode(stationname);
                }
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    @RequestMapping(value = "/edite", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edite(String data)
    {
        Map<String, String> map = new HashMap<String, String>();
        try
        {
            //data=java.net.URLDecoder.decode(data,"UTF-8");
            String [] list=decodeURL(data.split("&"));
            				for(int i=0;i<list.length;i++){
				    String[] array=list[i].split("=");
				    if(array.length==1){
				        map.put(array[0], "");
				    }else{
				        map.put(array[0], array[1]);
				    }
				}
				int retCount = businessCommonService.getCountByDeviceName(getLastCompanyId(),(long)AbstractController.中压站内断路器, map.get("devicename"),map.get("deviceid"));
				if(retCount>0){
				    map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "名称已存在！");
				    return map;
				}
				CompanyUser user=(CompanyUser) session.getAttribute("user");
            Zyzndlq device=new Zyzndlq();
            device.setDeviceid(map.get("deviceid"));
            device.setDevicename(map.get("devicename"));
            device.setAssetnumber(map.get("assetnumber"));
            device.setAssetsnature(map.get("assetsnature"));
            device.setAssetunit(map.get("assetunit"));
            device.setCompanyid(user.getLastCompanyId());
            device.setCreateid(user.getCompanyUserId());
            device.setDevicenumber(map.get("devicenumber"));
            device.setEquipmentstate(map.get("equipmentstate"));
            device.setImportantdegree(map.get("importantdegree"));
            device.setIsgenerationdimension(map.get("isgenerationdimension"));
            device.setIsruralpowernetwork(map.get("isruralpowernetwork"));
            device.setLastoperatorid(user.getCompanyUserId());
            device.setLinecode(map.get("linecode"));
            device.setLocalcity(map.get("localcity"));
            device.setMainlinecode(map.get("mainlinecode"));
            device.setMaintenanceteam(map.get("maintenanceteam"));
            device.setModel(map.get("model"));
            device.setNote(map.get("note"));
            device.setOperationstate(map.get("operationstate"));
            device.setOperationunit(map.get("operationunit"));
            device.setProductionfactory(map.get("productionfactory"));
            device.setSerialnumber(map.get("serialnumber"));
            device.setSyncstatus(0);
            device.setVoltagelevel(map.get("voltagelevel"));
            device.setCoilratedvoltage(new Long(PwtzUtils.passLong(map.get("coilratedvoltage"))));
            device.setCombinationdevicename(map.get("combinationdevicename"));
            device.setCombinationdevicetype(map.get("combinationdevicetype"));
            device.setCommissioningdate(PwtzUtils.parseDate(map.get("commissioningdate"), "yyyy-MM-dd"));
            device.setDropawaycurrent(PwtzUtils.passDouble(map.get("dropawaycurrent")));
            device.setGapcode(map.get("gapcode"));
            device.setNormalworkingmode(map.get("normalworkingmode"));
            device.setOperationmethods(map.get("operationmethods"));
            device.setOperatormodel(map.get("operatormodel"));
            device.setProjectname(map.get("projectname"));
            device.setProjectnumber(map.get("projectnumber"));
            device.setStationcode(map.get("stationcode"));
            device.setStationdevicetype(PwtzUtils.passInt(map.get("stationdevicetype")));
            device.setSwitcheffect(map.get("switcheffect"));
            device.setRatedcurrent(PwtzUtils.passDouble(map.get("ratedcurrent")));
            device.setRatedshortcircuitcur(PwtzUtils.passDouble(map.get("ratedshortcircuitcur")));
            device.setRatedshortcircuitnum(new Long(PwtzUtils.passLong(map.get("ratedshortcircuitnum"))));
            device.setRatedvoltage(map.get("ratedvoltage"));
            device.setRegionalcharacteristics(map.get("regionalcharacteristics"));
            device.setEquipmentadded(map.get("equipmentadded"));
            device.setEquipmentowner(map.get("equipmentowner"));
            device.setExtinguishingmedium(map.get("extinguishingmedium"));
            device.setFactorydate(PwtzUtils.parseDate(map.get("factorydate"), "yyyy-MM-dd"));
            int ret = service.update(device);
            if (ret > 0)
            {
                setLog(device.getDeviceid(), device.getCompanyid(), AbstractController.中压站内断路器, AbstractController.update, "修改中压站内断路器["+device.getDevicename()+"]成功", getUserId());
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "编辑中压站内断路器成功");
            }
            else
            {
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "编辑中压站内断路器失败");
            }
        }
        catch (PwtzDatabaseException e)
        {
            map = packageException(e);
        }
        catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping(value = "/load", method = RequestMethod.POST)
    @ResponseBody
    public Zyzndlq load(String deviceId) throws PwtzException
    {
        Zyzndlq zsbyq = null;
        try
        {
            if (null != deviceId)
            {
                zsbyq = service.load(deviceId);
            }
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return zsbyq;
    }
    
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(String data)
    {
        Map<String, String> map = new HashMap<String, String>();
        try
        {
            //data=java.net.URLDecoder.decode(data,"UTF-8");
            String [] list=decodeURL(data.split("&"));
            				for(int i=0;i<list.length;i++){
				    String[] array=list[i].split("=");
				    if(array.length==1){
				        map.put(array[0], "");
				    }else{
				        map.put(array[0], array[1]);
				    }
				}
				int retCount = businessCommonService.getCountByDeviceName(getLastCompanyId(),(long)AbstractController.中压站内断路器, map.get("devicename"));
				if(retCount>0){
				    map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "名称已存在！");
				    return map;
				}
				CompanyUser user=(CompanyUser) session.getAttribute("user");
            Zyzndlq device=new Zyzndlq();
            UUID uuid=UUID.randomUUID();
            device.setDeviceid(uuid.toString());
            device.setDevicename(map.get("devicename"));
            device.setAssetnumber(map.get("assetnumber"));
            device.setAssetsnature(map.get("assetsnature"));
            device.setAssetunit(map.get("assetunit"));
            device.setCompanyid(user.getLastCompanyId());
            device.setCreateid(user.getCompanyUserId());
            device.setDevicenumber(map.get("devicenumber"));
            device.setEquipmentstate(map.get("equipmentstate"));
            device.setImportantdegree(map.get("importantdegree"));
            device.setIsgenerationdimension(map.get("isgenerationdimension"));
            device.setIsruralpowernetwork(map.get("isruralpowernetwork"));
            device.setLastoperatorid(user.getCompanyUserId());
            device.setLinecode(map.get("linecode"));
            device.setLocalcity(map.get("localcity"));
            device.setMainlinecode(map.get("mainlinecode"));
            device.setMaintenanceteam(map.get("maintenanceteam"));
            device.setModel(map.get("model"));
            device.setNote(map.get("note"));
            device.setOperationstate(map.get("operationstate"));
            device.setOperationunit(map.get("operationunit"));
            device.setProductionfactory(map.get("productionfactory"));
            device.setSerialnumber(map.get("serialnumber"));
            device.setSyncstatus(0);
            device.setVoltagelevel(map.get("voltagelevel"));
            device.setCoilratedvoltage(new Long(PwtzUtils.passLong(map.get("coilratedvoltage"))));
            device.setCombinationdevicename(map.get("combinationdevicename"));
            device.setCombinationdevicetype(map.get("combinationdevicetype"));
            device.setCommissioningdate(PwtzUtils.parseDate(map.get("commissioningdate"), "yyyy-MM-dd"));
            device.setDropawaycurrent(PwtzUtils.passDouble(map.get("dropawaycurrent")));
            device.setGapcode(map.get("gapcode"));
            device.setNormalworkingmode(map.get("normalworkingmode"));
            device.setOperationmethods(map.get("operationmethods"));
            device.setOperatormodel(map.get("operatormodel"));
            device.setProjectname(map.get("projectname"));
            device.setProjectnumber(map.get("projectnumber"));
            device.setStationcode(map.get("stationcode"));
            device.setStationdevicetype(PwtzUtils.passInt(map.get("stationdevicetype")));
            device.setSwitcheffect(map.get("switcheffect"));
            device.setRatedcurrent(PwtzUtils.passDouble(map.get("ratedcurrent")));
            device.setRatedshortcircuitcur(PwtzUtils.passDouble(map.get("ratedshortcircuitcur")));
            device.setRatedshortcircuitnum(new Long(PwtzUtils.passLong(map.get("ratedshortcircuitnum"))));
            device.setRatedvoltage(map.get("ratedvoltage"));
            device.setRegionalcharacteristics(map.get("regionalcharacteristics"));
            device.setEquipmentadded(map.get("equipmentadded"));
            device.setEquipmentowner(map.get("equipmentowner"));
            device.setExtinguishingmedium(map.get("extinguishingmedium"));
            device.setFactorydate(PwtzUtils.parseDate(map.get("factorydate"), "yyyy-MM-dd"));
            device = service.add(device);
            if (device !=null)
            {
                setLog(device.getDeviceid(), device.getCompanyid(), AbstractController.中压站内断路器, AbstractController.add, "添加中压站内断路器["+device.getDevicename()+"]成功", getUserId());
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "添加中压站内断路器成功");
                map.put("deviceid", uuid.toString());
            }
            else
            {
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "添加中压站内断路器失败");
            }
        }
        catch (PwtzDatabaseException e)
        {
            map = packageException(e);
        }
        catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }
    @RequestMapping(value="dlist",method=RequestMethod.POST)
    @ResponseBody
    public List<Zyzndlq> getList(Long companyId){
        List<Zyzndlq> list=new ArrayList<Zyzndlq>();
        try{
            PaginationList<Zyzndlq> plist=service.list(null, companyId, 1, null, null,null, null, 0, 0);
            if(plist!=null&&plist.getTotalRecord()>0){
                list=plist.getRecords();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return list;
    }
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> delete(String deviceid){
        Map<String,String> map=new HashMap<String, String>();
        int result=0;
        try
        {
            Zyzndlq device=service.load(deviceid);
            if(device!=null)
            result=service.delete(deviceid);
            if (result>0)
            {
                setLog(device.getDeviceid(), device.getCompanyid(), AbstractController.中压站内断路器, AbstractController.delete, "删除中压站内断路器["+device.getDevicename()+"]成功", getUserId());
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "删除中压站内断路器成功");
            }
            else
            {
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "删除中压站内断路器失败");
            }
        }
        catch (PwtzDatabaseException e)
        {
            map = packageException(e);
        }
        catch (CanNotDeleteException e)
        {
            map = packageException(e);
        }
        return map;
    }
}
